
#import "APPlaceHolderTextView.h"

@interface APPlaceHolderTextView ()<UITextViewDelegate>
@end

@implementation APPlaceHolderTextView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        WS(ws);
        self.backgroundColor = LYRGBAColor(255, 255, 255, 0.3);
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _textView.delegate = self;
        _textView.font = [UIFont systemFontOfSize:15];
        _textView.textColor = UIColor.blackColor;
        _textView.returnKeyType = UIReturnKeyDone;
        _textView.backgroundColor = UIColor.clearColor;
        _textView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
        [self addSubview:_textView];
        [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(ws).with.insets(UIEdgeInsetsZero);
        }];
        _placeHolder = @"Enter your ideas...";
        _placeHolderLab = [[UILabel alloc] init];
        _placeHolderLab.textColor = LYRGBAColor(255, 255, 255, 0.5);
        _placeHolderLab.text = @"Enter your ideas...";
        _placeHolderLab.font = [LYFont fontWithSize:10 fontName:LYFontNamepfr];
        _placeHolderLab.userInteractionEnabled = NO;
        [_textView addSubview:_placeHolderLab];
        [_placeHolderLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(ws.textView.mas_top).with.offset(12);
            make.left.equalTo(ws.mas_left).with.offset(12);
        }];
        [self.textView addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)setIsPlaceHolderCenter:(_Bool)isPlaceHolderCenter{
    if (isPlaceHolderCenter) {
        [_placeHolderLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self.mas_left).with.offset(12);
        }];
    }

}

- (void)setPlaceHolder:(NSString *)placeHolder{
    _placeHolder = placeHolder;
    self.placeHolderLab.text = placeHolder;
}

- (void)setPlaceHolderColor:(UIColor *)placeHolderColor{
    _placeHolderColor = placeHolderColor;
    self.placeHolderLab.textColor = placeHolderColor;
}

- (void)setPlaceHolderFont:(UIFont *)placeHolderFont{
    _placeHolderFont = placeHolderFont;
    self.placeHolderLab.font = placeHolderFont;

}
- (void)setTextColor:(UIColor *)textColor{
    _textColor = textColor;
    self.textView.textColor = textColor;
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"text"]) {
        NSString *content = self.textView.text;
        if (content && content.length != 0) {
           self.placeHolderLab.text = @"";
        }else{
            self.placeHolderLab.text = _placeHolder;
        }
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    if (_textViewDidBeginInputBlock) {
        _textViewDidBeginInputBlock(textView);
    }
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}


- (void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        _placeHolderLab.text = _placeHolder;
    }else{
        self.placeHolderLab.text = @"";
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(placeHolderTextViewDelegateTextViewChanged:)]) {
        [self.delegate placeHolderTextViewDelegateTextViewChanged:textView];
    }
}


-(void)dealloc{
    
    [self.textView removeObserver:self forKeyPath:@"text" context:nil];
}
@end
